#include "ftlistmodel.h"
#include <QColor>

FtListModel::FtListModel()
{
    /* 建立信号和槽，启动定时器 */
    connect(&timer, &QTimer::timeout, this, &FtListModel::onTimeout);
    timer.start(500);
}

int FtListModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return 5;
}

int FtListModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return 1;
}

QVariant FtListModel::data(const QModelIndex &index, int role) const
{
    Q_UNUSED(index);
    QVariant value;
    switch (role)
    {
    case BodyColor:
        value = QColor(rand()%256, rand()%256, rand()%256, 255);
        break;
    }
    return value;
}

QHash<int, QByteArray> FtListModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles.insert(BodyColor, "BodyColor");
    return roles;
}

void FtListModel::onTimeout()
{
    QModelIndex topLeft = createIndex(0, 0);
    QModelIndex bottomRight = createIndex(5, 0);
    emit dataChanged(topLeft, bottomRight);
}
